package com.jingwee.springbootkafka.controller;

import com.jingwee.springbootkafka.producer.KafkaBaseProducer;
import com.jingwee.springbootkafka.template.KafkaBaseTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author jingwei
 * @version 1.0
 * @description
 * @date 2023/8/10
 */
@Slf4j
@RestController
@RequestMapping(value ="kafka")
public class KafkaController {

    @Resource
    private KafkaBaseProducer kafkaBaseProducer;

    @Resource
    private KafkaBaseTemplate kafkaBaseTemplate;

    /**
     * 创建主题
     *
     * @param topicName
     * @return
     */
    @GetMapping("/createTopic/{topic}")
    public String createTopic(@PathVariable("topic") String topicName) {
        return kafkaBaseTemplate.createTopic(topicName);
    }

    /**
     * 创建主题
     *
     * @param topicName
     * @return
     */
    @GetMapping("/deleteTopic/{topic}")
    public String deleteTopic(@PathVariable("topic") String topicName) {
        return kafkaBaseTemplate.deleteTopic(topicName);
    }

    /**
     * 创建主题
     *
     * @param topicName
     * @return
     */
    @GetMapping("/queryTopic/{topic}")
    public String queryTopic(@PathVariable("topic") String topicName) {
        return kafkaBaseTemplate.queryTopic(topicName);
    }

    /**
     * 发送消息
     *
     * @param topicName
     * @param message
     * @return
     */
    @GetMapping("/sendMessage/{topicName}/{message}")
    public String sendMessage(@PathVariable("topicName") String topicName, @PathVariable("message") String message) {
        return kafkaBaseProducer.sendStr(topicName, message);
    }
}
